home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GDEVBGI.C < prev    next >
C/C++ Source or Header  |  1993-05-12  |  19KB  |  652 lines

  1. /* Copyright (C) 1989, 1990, 1991, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevbgi.c */
  20. /* Ghostscript driver for Borland Graphics Interface (BGI) */
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <conio.h>
  24. #include <graphics.h>
  25. #include <mem.h>
  26. #include "gserrors.h"
  27. #include "gx.h"
  28. #include "gxdevice.h"
  29. #include "gxxfont.h"
  30.  
  31. #ifndef BGI_LIB                /* may be set in makefile */
  32. #  define BGI_LIB ""
  33. #endif
  34.  
  35. /*
  36.  * BGI supports these video cards:
  37.  *   Hercules, CGA, MCGA, EGA, VGA, AT&T 400, IBM 8514, PC3270.
  38.  * Highest resolution mode is used with all these video cards.
  39.  * EGA and VGA display 16 colors, the rest are black-and-white only.
  40.  * In addition, the environment variable BGIUSER may be used
  41.  * to define a user-supplied Super VGA driver: see the use.doc file
  42.  * for details.
  43.  */
  44. #define SUPER_VGA 999            /* bogus # for user-defined driver */
  45.  
  46. /* See gxdevice.h for the definitions of the procedures. */
  47.  
  48. dev_proc_open_device(bgi_open);
  49. dev_proc_close_device(bgi_close);
  50. dev_proc_map_rgb_color(bgi_map_rgb_color);
  51. dev_proc_map_color_rgb(bgi_map_color_rgb);
  52. dev_proc_fill_rectangle(bgi_fill_rectangle);
  53. dev_proc_tile_rectangle(bgi_tile_rectangle);
  54. dev_proc_copy_mono(bgi_copy_mono);
  55. dev_proc_copy_color(bgi_copy_color);
  56. dev_proc_draw_line(bgi_draw_line);
  57. dev_proc_get_xfont_procs(bgi_get_xfont_procs);
  58.  
  59. /* The device descriptor */
  60. typedef struct gx_device_bgi_s gx_device_bgi;
  61. struct gx_device_bgi_s {
  62.     gx_device_common;
  63.     int display_mode;
  64.     struct text_info text_mode;
  65. };
  66. #define bgi_dev ((gx_device_bgi *)dev)
  67. private gx_device_procs bgi_procs = {
  68.     bgi_open,
  69.     gx_default_get_initial_matrix,
  70.     gx_default_sync_output,
  71.     gx_default_output_page,
  72.     bgi_close,
  73.     bgi_map_rgb_color,
  74.     bgi_map_color_rgb,
  75.     bgi_fill_rectangle,
  76.     bgi_tile_rectangle,
  77.     bgi_copy_mono,
  78.     bgi_copy_color,
  79.     bgi_draw_line,
  80.     gx_default_get_bits,
  81.     gx_default_get_props,
  82.     gx_default_put_props,
  83.     NULL,
  84.     bgi_get_xfont_procs
  85. };
  86. gx_device_bgi gs_bgi_device = {
  87.     sizeof(gx_device_bgi),
  88.     &bgi_procs,
  89.     "bgi",
  90.     0, 0,        /* width and height are set in bgi_open */
  91.     1, 1,        /* density is set in bgi_open */
  92.     no_margins,
  93.     dci_black_and_white,
  94.     0        /* not open yet */
  95. };
  96.  
  97. /* Detection procedure for user-defined driver. */
  98. private int huge
  99. detectVGA(void)
  100. {    return gs_bgi_device.display_mode;
  101. }
  102.  
  103. /* Open the BGI driver for graphics mode */
  104. int
  105. bgi_open(gx_device *dev)
  106. {    int driver, mode;
  107.     char *bgi_user = getenv("BGIUSER");
  108.     char *bgi_path = getenv("BGIPATH");
  109.  
  110.     gettextinfo(&bgi_dev->text_mode);
  111.  
  112.     if ( bgi_path == NULL )
  113.         bgi_path = BGI_LIB;
  114.     if ( bgi_user != NULL )
  115.        {    /* A user-supplied driver is specified as "mode.dname", */
  116.         /* where mode is a hex number and dname is the name */
  117.         /* of the driver file. */
  118.         char dname[40];
  119.         if ( strlen(bgi_user) > sizeof(dname) ||
  120.              sscanf(bgi_user, "%x.%s", &mode, dname) != 2
  121.            )
  122.            {    eprintf("BGIUSER not in form nn.dname.\n");
  123.             exit(1);
  124.            }
  125.         gs_bgi_device.display_mode = mode;    /* sigh.... */
  126.         installuserdriver(dname, detectVGA);
  127.         driver = DETECT;
  128.         initgraph(&driver, &mode, bgi_path);
  129.         driver = SUPER_VGA;
  130.        }
  131.     else                /* not user-defined driver */
  132.        {    /* We include the CGA driver */
  133.         /* in the Ghostscript executable, so end-users don't */
  134.         /* have to have the BGI files. */
  135.         if ( registerfarbgidriver(CGA_driver_far) < 0 )
  136.            {    eprintf("BGI: Can't register CGA driver!\n");
  137.             exit(1);
  138.            }
  139.  
  140.         detectgraph(&driver, &mode);
  141.         if ( driver < 0 )
  142.            {    eprintf("BGI: No graphics hardware detected!\n");
  143.             exit(1);
  144.            }
  145.  
  146.         if ( driver == EGA64 )
  147.            {    /* Select 16 color video mode if video card is EGA with 64 Kb of memory */
  148.             mode = EGA64LO;
  149.            }
  150.  
  151.         /* Initialize graphics mode. */
  152.  
  153.         /* Following patch for AT&T 6300 is courtesy of */
  154.         /* Allan Wax, Xerox Corp. */
  155.         if ( driver == CGA )
  156.            {    /* The actual hardware might be an AT&T 6300. */
  157.             /* Try initializing it that way. */
  158.             int save_mode = mode;
  159.             driver = ATT400, mode = ATT400HI;
  160.             initgraph(&driver, &mode, bgi_path);
  161.             if ( graphresult() != grOk )
  162.                {    /* Nope, it was a real CGA. */
  163.                 closegraph();
  164.                 driver = CGA, mode = save_mode;
  165.                 initgraph(&driver, &mode, bgi_path);
  166.                }
  167.            }
  168.         else
  169.             initgraph(&driver, &mode, bgi_path);
  170.        }
  171.  
  172.        {    int code = graphresult();
  173.         if ( code != grOk )
  174.            {    eprintf1("Error initializing BGI driver: %s\n",
  175.                  grapherrormsg(code));
  176.             exit(1);
  177.            }
  178.        }
  179.  
  180.     /* Set parameters that were unknown before opening device */
  181.  
  182.     /* Size and nominal density of screen. */
  183.     /* The following algorithm maps an appropriate fraction of */
  184.     /* the display screen to an 8.5" x 11" coordinate space. */
  185.     /* This may or may not be what is desired! */
  186.     if ( dev->width == 0 )
  187.         dev->width = getmaxx() + 1;
  188.     if ( dev->height == 0 )
  189.         dev->height = getmaxy() + 1;
  190.     if ( dev->y_pixels_per_inch == 1 )
  191.        {    /* Get the aspect ratio from the driver. */
  192.         int arx, ary;
  193.         getaspectratio(&arx, &ary);
  194.         dev->y_pixels_per_inch = dev->height / 11.0;
  195.         dev->x_pixels_per_inch =
  196.             dev->y_pixels_per_inch * ((float)ary / arx);
  197.        }
  198.  
  199.     /* Find out if the device supports color */
  200.     /* (default initialization is monochrome). */
  201.     /* We only recognize 16-color devices right now. */
  202.     if ( getmaxcolor() > 1 )
  203.        {    static gx_device_color_info bgi_16color = dci_color(4, 2, 3);
  204.         dev->color_info = bgi_16color;
  205.        }
  206.     return 0;
  207. }
  208.  
  209. /* Close the BGI driver */
  210. int
  211. bgi_close(gx_device *dev)
  212. {    closegraph();
  213.     textmode(bgi_dev->text_mode.currmode);
  214.     return 0;
  215. }
  216.  
  217. /* Map a r-g-b color to the 16 colors available with an EGA/VGA video card. */
  218. gx_color_index
  219. bgi_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  220.   gx_color_value b)
  221. {    return (gx_color_index)
  222.         ((r > gx_max_color_value / 4 ? 4 : 0) +
  223.          (g > gx_max_color_value / 4 ? 2 : 0) +
  224.          (b > gx_max_color_value / 4 ? 1 : 0) +
  225.          (r > gx_max_color_value / 4 * 3 ||
  226.           g > gx_max_color_value / 4 * 3 ? 8 : 0));
  227. }
  228.  
  229. /* Map a color code to r-g-b.  Surprisingly enough, this is algorithmic. */
  230. int
  231. bgi_map_color_rgb(gx_device *dev, gx_color_index color,
  232.   gx_color_value prgb[3])
  233. {
  234. #define icolor (int)color
  235.     gx_color_value one =
  236.         (icolor & 8 ? gx_max_color_value : gx_max_color_value / 3);
  237.     prgb[0] = (icolor & 4 ? one : 0);
  238.     prgb[1] = (icolor & 2 ? one : 0);
  239.     prgb[2] = (icolor & 1 ? one : 0);
  240.     return 0;
  241. #undef icolor
  242. }
  243.  
  244. /* Copy a monochrome bitmap.  The colors are given explicitly. */
  245. /* Color = gx_no_color_index means transparent (no effect on the image). */
  246. int
  247. bgi_copy_mono(gx_device *dev,
  248.   const byte *base, int sourcex, int raster, gx_bitmap_id id,
  249.   int x, int y, int w, int h,
  250.   gx_color_index zero, gx_color_index one)
  251. {    const byte *ptr_line = base + (sourcex >> 3);
  252.     int left_bit = 0x80 >> (sourcex & 7);
  253.     int dest_y = y, end_x = x + w;
  254.     int invert = 0;
  255.     int color;
  256.  
  257.     if ( zero == gx_no_color_index )
  258.        {    if ( one == gx_no_color_index ) return 0;
  259.         color = (int)one;
  260.        }
  261.     else
  262.        {    if ( one == gx_no_color_index )
  263.            {    color = (int)zero;
  264.             invert = -1;
  265.            }
  266.         else
  267.            {    /* Pre-clear the rectangle to zero */
  268.             setfillstyle(SOLID_FILL, (int)zero);
  269.             bar(x, y, x + w - 1, y + h - 1);
  270.             color = (int)one;
  271.            }
  272.        }
  273.  
  274.     while ( h-- )              /* for each line